package 多线程轮流打印字母类试题;

/**
 *两个线程交替打印AB,总共10次
 */
public class B {

    private static int i=0;
    private static int flag=1;

    public static void main(String[] args) {


        Runnable runnable=new Runnable() {
            @Override
            public void run() {
                synchronized (this){
                    try{
                        int threadid=Integer.valueOf(Thread.currentThread().getName());
                        while (i<10){
                          if(i%2==threadid-1){
                              System.out.println(threadid+"号线程打印"+(flag==1?'A':'B'));
                              i++;
                              flag=-flag;
                              notifyAll();
                          }else {
                              wait();
                          }

                        }
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
            }
        };
        Thread t1=new Thread(runnable,"1");



    }
}
